#include <stdio.h>
#include <sys/statvfs.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <linux/fs.h>
#include <fcntl.h>

void get_disk_info(const char* path, const char* device) {
    // 文件系统信息
    struct statvfs fs_stat;
    if (statvfs(path, &fs_stat) == 0) {
        printf("文件系统信息:\n");
        printf("总空间: %.2f GB\n", 
               (double)fs_stat.f_blocks * fs_stat.f_bsize / (1024*1024*1024));
        printf("可用空间: %.2f GB\n", 
               (double)fs_stat.f_bavail * fs_stat.f_bsize / (1024*1024*1024));
    }
    
    // 设备信息
    int fd = open(device, O_RDONLY);
    if (fd >= 0) {
        unsigned long long size;
        if (ioctl(fd, BLKGETSIZE64, &size) == 0) {
            printf("\n设备信息:\n");
            printf("设备总大小: %.2f GB\n", (double)size / (1024*1024*1024));
        }
        close(fd);
    }
}
// 计算TF卡剩余录像时间
double calculate_recording_time(const char* mount_path, unsigned int bitrate_kbps) {
    struct statvfs stat;
    
    // 获取文件系统信息
    if (statvfs(mount_path, &stat) != 0) {
        perror("获取存储信息失败");
        return -1.0;
    }
    
   
    // 计算可用空间（字节）
    unsigned long long total_space = (unsigned long long)stat.f_blocks * stat.f_bsize;
    unsigned long long free_space = (unsigned long long)stat.f_bsize * stat.f_bavail;
    
    // 将码率从kbps转换为Bytes/s
    unsigned long long bytes_per_second = (unsigned long long)bitrate_kbps * 1024 / 8;
    
    printf("存储卡总容量: %.2f GB\n", total_space / (1024.0 * 1024.0 * 1024.0));
    printf("存储卡剩余容量: %.2f GB\n", free_space / (1024.0 * 1024.0 * 1024.0));
    // 计算可录制时间（秒）
    double recording_time = (double)free_space / bytes_per_second;
    
    return recording_time;
}

// 格式化显示录像时间
void display_recording_time(double seconds) {
    int hours = (int)(seconds / 3600);
    int minutes = (int)((seconds - hours * 3600) / 60);
    int secs = (int)(seconds - hours * 3600 - minutes * 60);
    
    printf("剩余可录制时间: %02d:%02d:%02d\n", hours, minutes, secs);
}

// 示例使用
void check_storage_status(const char* tf_card_path, unsigned int bitrate_kbps) {
    double remaining_time = calculate_recording_time(tf_card_path, bitrate_kbps);
    
    if (remaining_time < 0) {
        printf("无法获取存储信息\n");
        return;
    }
    
    display_recording_time(remaining_time);
    
    // 显示剩余空间警告
    if (remaining_time < 300) { // 少于5分钟
        printf("警告：存储空间不足！\n");
    }
}
// ... 保持之前的代码不变 ...

int main(int argc, char *argv[]) {
    const char *tf_card_path = "/mnt/tf";  // TF卡挂载路径，根据实际情况修改
    unsigned int bitrate_kbps = 4096;      // 默认码率4Mbps，根据实际需求修改
    
    // 如果命令行提供了参数
    if (argc > 1) {
        tf_card_path = argv[1];  // 第一个参数作为TF卡路径
    }
    if (argc > 2) {
        bitrate_kbps = (unsigned int)atoi(argv[2]);  // 第二个参数作为码率
    }
    
    printf("TF卡路径: %s\n", tf_card_path);
    printf("视频码率: %d kbps\n", bitrate_kbps);
    
    // e 盘
    get_disk_info("/mnt/e/", "/dev/mmcblk0");  // TF卡通常的设备名

    // 当前盘
    get_disk_info("/", "/dev/root");  // 或 "/dev/sda1" 等，具体要看您的系统
    // 定期检查存储状态
    while (1) {
        check_storage_status(tf_card_path, bitrate_kbps);
        sleep(5);  // 每5秒更新一次
    }
    
    return 0;
}